const express = require("express")
const app = express()
const bodyParser = require('body-parser')
const cors = require('cors')
const multipart = require('connect-multiparty');
const Router = require('./routes/router.js')
const checkToken = require('./utils/checkToken.js')

app.use(cors())
app.use(express.static('public'))


app.all('*', function (req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
  res.setHeader('Access-Control-Allow-Headers', 'x-www-form-urlencoded', 'X-Requested-With,content-type, Authorization');
  res.setHeader("Content-Type", "application/json;charset=utf-8");
  next();
})


app.use(bodyParser.urlencoded({ extended: false }));// 解析 url-encoded格式的表单数据
app.use(bodyParser.json());// 解析json格式的表单数据
app.use(multipart());//解析form-data提交数据

app.use(checkToken)

app.use('/api', Router)

//通常会在所有的路由之后配置处理404的内容
app.use((req, res, next) => {

  res.status(404).send('<h1>404 Not Found.</h1>')

})

app.listen(8082, () => {
  console.log("server running at http://127.0.0.1:8082")
})